var utils = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    address: "请选择",
    time: "请选择",
    date: "请选择",
    index: 0,
    unitPrice: 0,
    totalPrice: 0,
    purchaseCount: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    productId: null,
    productInfo: [],
    message: '',
    size: '',
    hiddenmodalput: true,
    cellphone: '',
    wechatNumber: '',
  },
  onLoad: function (options) {
    var _this = this;
    this.setData({
      index: options.index,
      totalPrice: options.totalPrice,
      unitPrice: options.unitPrice,
      productId: options.id,
      size: options.size
    })

    wx.request({
      url: 'https://cake.cindata.cn/product/getProductInfoById',
      data: {
        productId: options.id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      complete: function complete(res) {
        if (res.statusCode == 200) {
          _this.setData({
            productInfo: res.data
          })
        } else {
          console.log("获取商品详情失败，请稍后重试！");
        }
      }
    })
  },
  onShow: function (options) {
    var _this = this
    if (app.globalData.addressId) {
      wx.request({
        url: 'https://cake.cindata.cn/address/getAddressById',
        data: {
          id: app.globalData.addressId
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        complete: function complete(res) {
          if (res.statusCode == 200) {
            _this.setData({
              address: res.data
            })
          } else {
            console.log("请求收货地址失败，请稍后重试！");
          }
        }
      });
    }
  },
  changeMessage: function(e) {
    if (e.detail.value) {
      this.setData({
        message: e.detail.value
      })
    }
  },
  bindAddressCahnge: function (e) {
    wx.navigateTo({
      url: '/pages/myAddress/myAddress'
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindCashOnDelivery: function (e) {

    var _this = this
    
    // 查看用户信息是否完整
    wx.request({
      url: 'https://cake.cindata.cn/contact/getContactByOpenId',
      data: {
        id: app.globalData.openId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      complete: function complete(res) {
        if (res.statusCode == 200) {
          if (res.data.wechatNumber == '' || res.data.wechatNumber == null) {
            _this.setData({
              hiddenmodalput: false
            })
          } else {
            _this.setData({
              hiddenmodalput: true
            })
            _this.submit()
          }
        }
      }
    });
  },
  submit: function () {
    var _this = this
    // 参数校验
    var addressId = app.globalData.addressId
    var warn = "";//弹框时提示的内容
    var flag = true;//判断信息输入是否完整
    if (!addressId) {
      warn = "请选择地址";
    } else if (_this.data.date < utils.formatTime(new Date())) {
      warn = "送达时间不能小于当前时间";
    } else {
      flag = false;
      wx.showModal({
        title: '确认订购？',
        content: '订购成功后，我们的客服会尽快和您取得联系，请稍后！',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: 'https://cake.cindata.cn/opportunity/createOpportunity',
              data: {
                openId: app.globalData.openId,
                addressId: addressId,
                unitPrice: _this.data.unitPrice,
                totalPrice: _this.data.totalPrice,
                purchaseCount: _this.data.purchaseCount[_this.data.index],
                productId: _this.data.productId,
                message: _this.data.message,
                date: _this.data.date,
                size: _this.data.size
              },
              method: 'POST',
              header: {
                'content-type': 'application/json'
              },
              complete: function complete(res) {
                if (res.statusCode == 200) {
                  wx.showModal({
                    title: '恭喜您',
                    content: '订购成功',
                    showCancel: false,
                    success: function (res) {
                      wx.navigateTo({
                        url: '/pages/order/order'
                      })
                    }
                  })
                } else {
                  console.log("提交订单失败，请稍后重试！");
                }
              }
            });
          }
        }
      })
    }

    //如果信息填写不完整，弹出输入框  
    if (flag == true) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: warn
      })
    }
  },
  bindweiChatPay: function () {
    wx.showModal({
      title: '提示',
      content: '购买成功',
      showCancel: false,
      success: function (res) {
        wx.switchTab({
          url: '/pages/home/home'
        })
      }
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      totalPrice: this.data.unitPrice * this.data.purchaseCount[e.detail.value]
    })
  },
  changeWechatNumber: function (e) {
    console.log(e.detail.value);
    if (e.detail.value) {
      this.setData({
        wechatNumber: e.detail.value
      })
    }
  },
  changeCellphone: function (e) {
    console.log(e.detail.value);
    if (e.detail.value) {
      this.setData({
        cellphone: e.detail.value
      })
    }
  },
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  confirm: function (e) {
    var _this = this
    var warn = "";
    var flag = true;
    if (this.data.wechatNumber.trim() == '') {
      warn = "请输入微信号";
    } else if (this.data.cellphone.trim() && !(/^1[0-9]{10}$/.test(this.data.cellphone.trim()))) {
      warn = "手机号码格式不正确";
    } else {
      flag = false;
      wx.request({
        url: 'https://cake.cindata.cn/contact/editContact',
        data: {
          openId: app.globalData.openId,
          wechatNumber: this.data.wechatNumber,
          cellphone: this.data.cellphone
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        complete: function complete(res) {
          if (res.statusCode == 200) {
            wx.showModal({
              title: '恭喜您',
              showCancel: false,
              content: "信息完善成功"
            })

            _this.cancel()
          }
        }
      });
    }

    //如果信息填写不完整，弹出输入框  
    if (flag == true) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: warn
      })
    }
  },
})